class Solution
{
public:
    int countDistinct(vector<int> &nums, int k, int p)
    {
        set<vector<int>> suitable;
        int n = nums.size();
        vector<int> divisibleCount(n + 1, 0);
        for (int i = 0; i < n; ++i)
        {
            if (nums[i] % p == 0)
            {
                divisibleCount[i + 1] = divisibleCount[i] + 1;
            }
            else
            {
                divisibleCount[i + 1] = divisibleCount[i];
            }
        }

        for (int i = 0; i < n; ++i)
        {
            for (int j = i + 1; j <= n; ++j)
            {
                if (divisibleCount[j] - divisibleCount[i] <= k)
                {
                    suitable.emplace(nums.begin() + i, nums.begin() + j);
                }
            }
        }
        return suitable.size();
    }
};